package com.qdsgjsfk.vision.ui.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * Created by HP on 2019/11/29.
 */

@SuppressLint("AppCompatCustomView")
public class MyImageView extends ImageView{
    GestureDetector gd;
    Context context;

    public MyImageView(Context context) {
        super(context);
        this.context = context;
        gd = new GestureDetector(context, listener);
    }

    public MyImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        gd = new GestureDetector(context, listener);

    }

    public MyImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        gd = new GestureDetector(context, listener);

    }

    public MyImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.context = context;
        gd = new GestureDetector(context, listener);
    }

//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        System.out.println("cccccccccccc");
//        return gd.onTouchEvent(event);
//    }

    private int verticalMinDistance = 20;
    private int minVelocity = 0;

    GestureDetector.SimpleOnGestureListener listener = new GestureDetector.SimpleOnGestureListener() {

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

            System.out.println("aaaaaaaaaaaaa");
            if (e1.getX() - e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {

                // 切换Activity
                // Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class);
                // startActivity(intent);
                Toast.makeText(context, "向左手势", Toast.LENGTH_SHORT).show();
            } else if (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {

                // 切换Activity
                // Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class);
                // startActivity(intent);
                Toast.makeText(context, "向右手势", Toast.LENGTH_SHORT).show();
            }

            return false;
        }

    };
}
